

class EasyTranslateController {
    
    // these will be injected by Griffon
    def model
    def view
    org.fasttranslate.TranslationService translationService
    
    void mvcGroupInit(Map args) {
        // this method is called after model and view are injected
    }

    def action = {evt = null ->
        
        doOutside {
            
            try {
                model.busy = true
                model.result1Busy = true 
                model.result2Busy = true    
                
                translationService.translate(model.textSource, [
                    [from: model.language2, 
                       to: model.language1, 
                     then: { result -> edt { model.lang1Result = result; model.result1Busy = false }}], 
                    
                    [from: model.language1, 
                       to: model.language2, 
                     then: { result -> edt { model.lang2Result = result; model.result2Busy = false }}]]
                )
            } catch (Throwable t) {
                t.printStackTrace()
                edt {
                    model.lang1Result = t.message
                    model.lang2Result = t.message
                }
            } finally {
                edt {
                    view.textbox.selectAll()
                    model.busy = false
                    model.result1Busy = false
                    model.result2Busy = false   
                }
            }
        }
    }
}
